<?php

namespace App\Http\Controllers;

use App\Models\Cart;
use App\Models\Goods;
use Illuminate\Http\Request;

class CartController extends ApiController
{


    //购物车信息
    public function list(Request $request)
    {
        $userId = $request['userId'];
        $list               = $this->getValidCartList($userId);
        $goodsCount         = 0;
        $goodsAmount        = 0;
        foreach ($list as $item) {
            $goodsCount  += $item->number;
            $amount      = bcmul($item->number, $item->price, 2);
            $goodsAmount = bcadd($goodsAmount, $amount, 2);
        }
        return self::returnAjax([
            'cartList'  => $list->toArray(),
            'cartTotal' => [
                'goodsCount'         => $goodsCount,
                'goodsAmount'        => (double) $goodsAmount,
            ]
        ], 200);
    }


    public function restart($uid)
    {
        $userId = $uid;
        $list               = $this->getValidCartList($userId);
        $goodsCount         = 0;
        $goodsAmount        = 0;
        foreach ($list as $item) {
            $goodsCount  += $item->number;
            $amount      = bcmul($item->number, $item->price, 2);
            $goodsAmount = bcadd($goodsAmount, $amount, 2);
        }
        return self::returnAjax([
            'cartList'  => $list->toArray(),
            'cartTotal' => [
                'goodsCount'         => $goodsCount,
                'goodsAmount'        => (double) $goodsAmount,
            ]
        ], 200);
    }

    //获取购物车有效数据
    public function getValidCartList($userId)
    {
        $lists          = $this->getCartList($userId);
        return $lists;
    }

    /**
     * @param $ids
     * @return Goods[]|Builder[]|Collection
     * 根据商品的id,获取商品的列表
     */
    public function getGoodsListByIds($ids)
    {
        return Goods::query()->whereIn('id', $ids)->get();
    }


    /**
     * @param $userId
     * @return Cart[]|Builder[]|Collection
     * 获取用户购物车的数据
     */
    public function getCartList($userId)
    {
        return Cart::query()->where('user_id',$userId)->get();
    }

    //加入购物车
    public function add(Request $request){
        $goodId = $request['id'];
        $number = $request['num'];
        $userId = $request['userId'];

        //得到商品
        $goodInfo = Goods::query()->where('id',$goodId)->first();
        if (is_null($goodInfo)) {
            return parent::ajaxReturn(701, new \stdClass(), '当前商品不存在');
        }
        //判断当前商品在不在购物车再更新不在新增
        $cartProduct = Cart::query()->where('user_Id',$userId)->where('good_id',$goodId)->first();

        if (is_null($cartProduct)) {
            $this->newCart($userId, $goodInfo, $number);
        } else {
            $num = $number + $cartProduct->number;
            $this->editCartNum($cartProduct, $goodInfo, $num);
        }
        $count = $this->countCartProduct($userId);
        return self::returnAjax($count, 200);
    }

    public function editCartNum($cartProduct, $goodInfo, $num)
    {
        if ($num > $goodInfo->number) {
            return parent::ajaxReturn(702, new \stdClass(), '商品昆库存不足');
        }
        $cartProduct->number = $num;
        $cartProduct->save();
        return $cartProduct;
    }

    public function newCart($userId, $goodInfo, $number){
        $cart = new Cart();
        if ($number > $goodInfo->number) {
            return parent::ajaxReturn(702, new \stdClass(), '商品昆库存不足');
        };
        $cart->goods_name     = $goodInfo->good_name;
        $cart->pic_url        = $goodInfo->good_pic;
        $cart->price          = $goodInfo->good_price;
        $cart->good_id       = $goodInfo->id;
        $cart->checked        = true;
        $cart->user_id        = $userId;
        $cart->number         = $number;
        $cart->save();
        return $cart;
    }


    public function countCartProduct($userId)
    {
        return Cart::query()->where('user_id',$userId)->sum('number');
    }

    public function delete(Request $request)
    {
        $userId = $request['userId'];
        $productIds = $request['productIds'];
        Cart::query()->where('user_id', $userId)->where('good_id', $productIds)->delete();
        return $this->restart($userId);
    }
}
